package com.lsl.bhllserver.service.order;

import java.util.Calendar;
import java.util.List;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.lsl.bhllserver.entity.Order;
import com.lsl.bhllserver.entity.Product;
import com.lsl.bhllserver.service.func.IOrderBulider;

@Service
public class CusOrderService extends OrderService{

    @CacheEvict(value = "cusMyOrder", key = "#ob.build().getCusId()")
    public boolean orderCheckOut(IOrderBulider ob) {
        Order temp = ob.build();
        temp.setOrderDate(Calendar.getInstance().getTime());
        temp.setOrderStatus(0);
        temp.setOrderPostage(0);
        temp.setOrderPostman("(未填写)");
        Product prodtemp = productDAO.findByID(temp.getProdId().toString());
        if (prodtemp.getProdCount() < temp.getOrderBuyCount()) {
            return false;   
        }
        prodtemp.setProdCount(prodtemp.getProdCount() - temp.getOrderBuyCount());
        productDAO.update(prodtemp.getId(), prodtemp);
        return orderDAO.insert(temp);
    }

    @CacheEvict(value = "cusMyOrder", key = "#ob.build().getCusId()")
    public boolean finishOrder(IOrderBulider ob) {
        Order temp = ob.build();
        temp.setOrderFinishDate(Calendar.getInstance().getTime());
        temp.setOrderStatus(3);
        return orderDAO.update(temp.getId(), temp);
    }

    @Cacheable(value = "cusMyOrder", key = "#userid")
    public List<Order> getMyOrder(Integer userid) {
        return orderDAO.findByCusID(String.valueOf(userid));
    }
}
